/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   StdDataTypesCoderTest.cpp. Created on: 10.06.2009 13:52:07
*/


#include "cpputils/TestUtils.hpp"
#include "smartobjects/coders/StdNetworkCommand.hpp"
#include "smartobjects/coders/StdDataTypesCoder.hpp"
#include "smartobjects/coders/CallInterfaceOperationRequest.hpp"
#include "smartobjects/coders/CallInterfaceOperationResponse.hpp"
#include "smartobjects/coders/HandshakeRequest.hpp"
#include "smartobjects/coders/HandshakeResponse.hpp"


using namespace smartobjects;

BOOST_AUTO_TEST_CASE ( TestStdNetworkCommand ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	StdNetworkCommand poCommand ( 0xCC, poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");

	poCommand.beginSerialize();
	poCommand.getBuffer()->append(0xDD);
	poCommand.finishSerialize();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poCommand.getDestination() == poDecodeCommand.getDestination() );
	BOOST_REQUIRE ( poCommand.getInstanceId() == poDecodeCommand.getInstanceId() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	unsigned char nUserByte = *poDecodeCommand.getBuffer()->getDataAtPos();
	poDecodeCommand.getBuffer()->seek(1);
	BOOST_REQUIRE ( nUserByte == 0xDD );
}

BOOST_AUTO_TEST_CASE ( TestCallInterfaceOperationRequest ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	CallInterfaceOperationRequest poCommand ( poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");
	poCommand.setOperationId("HelloOp");

	poCommand.beginSerialize();
	poCommand.getBuffer()->append(0xDD);
	poCommand.finishSerialize();

	// poCommand.getBuffer()->dump();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poCommand.getDestination() == poDecodeCommand.getDestination() );
	BOOST_REQUIRE ( poCommand.getInstanceId() == poDecodeCommand.getInstanceId() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	CallInterfaceOperationRequest poDecodedRequestCommand ( &poDecodeCommand );
	poDecodedRequestCommand.deserialize();
	BOOST_REQUIRE ( poCommand.getOperationId() == poDecodedRequestCommand.getOperationId() );

	unsigned char nUserByte = *poDecodeCommand.getBuffer()->getDataAtPos();
	poDecodeCommand.getBuffer()->seek(1);
	BOOST_REQUIRE ( nUserByte == 0xDD );
}

BOOST_AUTO_TEST_CASE ( TestCallInterfaceOperationResponse ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	CallInterfaceOperationResponse poCommand ( poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");

	poCommand.beginSerialize();
	poCommand.getBuffer()->append(0xDD);
	poCommand.finishSerialize();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poDecodeCommand.getDestination().empty() );
	BOOST_REQUIRE ( poDecodeCommand.getInstanceId().empty() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	CallInterfaceOperationResponse poDecodedRequestCommand ( &poDecodeCommand );
	poDecodedRequestCommand.deserialize();
	BOOST_REQUIRE ( poCommand.isExceptionFlag() == poDecodedRequestCommand.isExceptionFlag() );

	unsigned char nUserByte = *poDecodeCommand.getBuffer()->getDataAtPos();
	poDecodeCommand.getBuffer()->seek(1);
	BOOST_REQUIRE ( nUserByte == 0xDD );
}

BOOST_AUTO_TEST_CASE ( TestCallInterfaceOperationResponseError ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	CallInterfaceOperationResponse poCommand ( poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");
	poCommand.setExceptionFlag(true);
	poCommand.setExceptionInterfaceId("HelloIntf");
	poCommand.setExceptionMessage("HelloEx");

	poCommand.beginSerialize();
	poCommand.finishSerialize();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poDecodeCommand.getDestination().empty() );
	BOOST_REQUIRE ( poDecodeCommand.getInstanceId().empty() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	CallInterfaceOperationResponse poDecodedRequestCommand ( &poDecodeCommand );
	poDecodedRequestCommand.deserialize();
	BOOST_REQUIRE ( poCommand.isExceptionFlag() == poDecodedRequestCommand.isExceptionFlag() );
	BOOST_REQUIRE ( poCommand.getExceptionInterfaceId() == poDecodedRequestCommand.getExceptionInterfaceId() );
	BOOST_REQUIRE ( poCommand.getExceptionMessage() == poDecodedRequestCommand.getExceptionMessage() );
}

BOOST_AUTO_TEST_CASE ( TestHandshakeRequest ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	HandshakeRequest poCommand ( poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");
	for(int i=0; i<100;i++) {
		poCommand.getHandshakeData().push_back(0xC1);
		poCommand.getHandshakeData().push_back(0xC2);
		poCommand.getHandshakeData().push_back(0xC3);
	}

	poCommand.beginSerialize();
	poCommand.finishSerialize();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poCommand.getDestination() == poDecodeCommand.getDestination() );
	BOOST_REQUIRE ( poCommand.getInstanceId() == poDecodeCommand.getInstanceId() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	HandshakeRequest poDecodedRequestCommand ( &poDecodeCommand );
	poDecodedRequestCommand.deserialize();
	BOOST_REQUIRE ( poCommand.getHandshakeData().size() == poDecodedRequestCommand.getHandshakeData().size() );
	for(unsigned int i=0;i<poCommand.getHandshakeData().size();i++)
		BOOST_REQUIRE ( poCommand.getHandshakeData()[i] == poDecodedRequestCommand.getHandshakeData()[i] );

}

BOOST_AUTO_TEST_CASE ( TestHandshakeResponse ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	HandshakeResponse poCommand ( poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");

	for(int i=0; i<100;i++) {
		poCommand.getHandshakeData().push_back(0xC1);
		poCommand.getHandshakeData().push_back(0xC2);
		poCommand.getHandshakeData().push_back(0xC3);
	}

	poCommand.beginSerialize();
	poCommand.finishSerialize();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poDecodeCommand.getDestination().empty() );
	BOOST_REQUIRE ( poDecodeCommand.getInstanceId().empty() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	HandshakeResponse poDecodedRequestCommand ( &poDecodeCommand );
	poDecodedRequestCommand.deserialize();
	BOOST_REQUIRE ( poCommand.isError() == poDecodedRequestCommand.isError() );

	BOOST_REQUIRE ( poCommand.getHandshakeData().size() == poDecodedRequestCommand.getHandshakeData().size() );
	for(unsigned int i=0;i<poCommand.getHandshakeData().size();i++)
		BOOST_REQUIRE ( poCommand.getHandshakeData()[i] == poDecodedRequestCommand.getHandshakeData()[i] );

}

BOOST_AUTO_TEST_CASE ( TestHandshakeResponseError ) {
	boost::intrusive_ptr<IDataTypesCoder> poCoder( new StdDataTypesCoder(), false );
	boost::intrusive_ptr<ByteBuffer> poBuffer (new ByteBuffer(1024), false);
	HandshakeResponse poCommand ( poBuffer.get(), poCoder.get() );
	poCommand.setMessageId(0xE1E2E3E4);
	poCommand.setDestination("Hello");
	poCommand.setError(true);
	poCommand.setErrorMessage("HelloError");

	poCommand.beginSerialize();
	poCommand.finishSerialize();

	BOOST_REQUIRE ( poCommand.getBuffer()->getLen() == poCommand.getCommandBodySize() + poCommand.getHeaderSize() );

	StdNetworkCommand poDecodeCommand ( poBuffer.get(), poCoder.get() );
	poDecodeCommand.deserialize();

	BOOST_REQUIRE ( poCommand.getCommandId() == poDecodeCommand.getCommandId() );
	BOOST_REQUIRE ( poCommand.getCommandBodySize() == poDecodeCommand.getCommandBodySize() );
	BOOST_REQUIRE ( poDecodeCommand.getDestination().empty() );
	BOOST_REQUIRE ( poDecodeCommand.getInstanceId().empty() );
	BOOST_REQUIRE ( poCommand.getMessageId() == poDecodeCommand.getMessageId() );
	BOOST_REQUIRE ( poCommand.getProtocolVersion() == poDecodeCommand.getProtocolVersion() );

	HandshakeResponse poDecodedRequestCommand ( &poDecodeCommand );
	poDecodedRequestCommand.deserialize();
	BOOST_REQUIRE ( poCommand.isError() == poDecodedRequestCommand.isError() );
	BOOST_REQUIRE ( poCommand.getErrorMessage() == poDecodedRequestCommand.getErrorMessage() );
	BOOST_REQUIRE ( poCommand.getHandshakeData().size() == 0 && poDecodedRequestCommand.getHandshakeData().size() == 0 );

}
